using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Upgrade;

namespace Lapointe.WebPartPageHistory.ProvisioningHelpers
{
    class ProvisioningHelper
    {
        /// <summary>
        /// Adds a list to the specified list collection.
        /// </summary>
        /// <param name="lists">The lists collection to add the list to.</param>
        /// <param name="urlName">Name of the list to use in the URL.</param>
        /// <param name="title">The title of the list.</param>
        /// <param name="description">The description.</param>
        /// <param name="featureId">The feature id that the list template is associated with.</param>
        /// <param name="templateType">Type of the template.</param>
        /// <param name="newListCreated">if set to <c>true</c> [new list created].</param>
        /// <returns></returns>
        internal static SPList AddList(SPListCollection lists, string urlName, string title, string description, Guid featureId, SPListTemplateType templateType, out bool newListCreated)
        {
            return AddList(lists, urlName, title, description, featureId, (int)templateType, out newListCreated);
        }



        /// <summary>
        /// Adds a list to the specified list collection.
        /// </summary>
        /// <param name="lists">The lists collection to add the list to.</param>
        /// <param name="urlName">Name of the list to use in the URL.</param>
        /// <param name="title">The title of the list.</param>
        /// <param name="description">The description.</param>
        /// <param name="featureId">The feature id that the list template is associated with.</param>
        /// <param name="templateType">Type of the template.</param>
        /// <param name="newListCreated">if set to <c>true</c> [new list created].</param>
        /// <returns></returns>
        private static SPList AddList(SPListCollection lists, string urlName, string title, string description, Guid featureId, int templateType, out bool newListCreated)
        {
            SPList listByUrl = null;
            newListCreated = false;
            try
            {
                listByUrl = CommonUtilities.GetListByUrl(lists.Web, urlName);
            }
            catch (SPException)
            {
            }
            catch (FileNotFoundException)
            {
            }
            if (listByUrl != null)
            {
                if (!listByUrl.TemplateFeatureId.Equals(featureId))
                {
                    if (!SPManager.PeekIsUpgradeRunning())
                    {
                        throw new SPException("An error occured provisioning the list.");
                    }
                    string serverRelativeUrl = listByUrl.RootFolder.ServerRelativeUrl;
                    string moveToUrl = CommonUtilities.GetUniqueObjectUrl(lists.Web.Site, serverRelativeUrl, null, -1);
                    CommonUtilities.ConfirmNotNullOrEmptyString(moveToUrl, "moveToUrl");
                    listByUrl.RootFolder.MoveTo(moveToUrl);
                    listByUrl = null;
                }
            }
            if (listByUrl == null)
            {
                title = CommonUtilities.GetUniqueListTitle(lists, title);
                Guid guid = lists.Add(title, description, urlName, featureId.ToString("D"), templateType, null);
                newListCreated = true;
                listByUrl = lists[guid];
            }
            return listByUrl;
        }
    }
}